package club.hiuit.share.kuaidi100.mvc.controller;


import club.hiuit.share.kuaidi100.entity.KuaidiProperties;
import club.hiuit.share.kuaidi100.handle.Kuaidi100Handle;
import club.hiuit.share.kuaidi100.util.Kuaidi100MD5;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static java.util.Objects.requireNonNull;

@Controller
@Slf4j
@RequestMapping("/${kuaidi100.rec:kuadi100}")
public class Kuaidi100Controller {


    private KuaidiProperties kuaidiProperties;


    private Kuaidi100Handle kuaidi100Handle;


    //签名是否校验
    private boolean errorSign;

    /**
     *
     * 快递100回调处理
     */
    @RequestMapping(value = "/kuaidiNum")
    @ResponseBody
    public String kuaidiNum(@RequestBody String string) {
        if (StringUtils.isBlank(string)) {
            return null;
        }

        requireNonNull(kuaidi100Handle,"Kuaidi100Handle实现不存在，请实现Kuaidi100Handle接口或除去本依赖包");

        JSONObject jsonObject = JSONObject.parseObject(string);
        String appid = jsonObject.getString("appid");
        String appuid = jsonObject.getString("appuid");
        String timestamp = jsonObject.getString("timestamp");
        String type = jsonObject.getString("type");
        String sign_from = jsonObject.getString("sign");
        String respones = null;
        log.info("===appid:{}====\n"+
                "===appuid:{}====\n"+
                "===timestamp:{}====\n"+
                "===type:{}====\n"+
                "===sign_from:{}====\n"+
                "===data:{}====\n", appid,appuid,timestamp,type,sign_from,jsonObject.getString("data"));
        if (StringUtils.isNotBlank(appid) && StringUtils.isNotBlank(appuid) && StringUtils.isNotBlank(timestamp) &&
            StringUtils.isNotBlank(type)) {
            String sign = Kuaidi100MD5.encode(kuaidiProperties.getAppsecret() + Kuaidi100MD5.encode(appid + timestamp + appuid));
            if ( StringUtils.isBlank(sign) ){
                log.info("=====签名加密，请检查签名配置=====");
                return "{\"status\":200}";
            }
            log.info("=========sign====>>{}<<",sign);
            boolean equals = sign.equals(sign_from);
            log.info("======sign-key========",equals);
            //签名正确
            if (equals || kuaidiProperties.isErrorSign()){
                //导入回调信息
                if ("SEND".equals(type)){
                    log.info("=========SEND=========");
                    JSONObject data = jsonObject.getJSONObject("data");
                    respones = kuaidi100Handle.send(data);
                }
                //修改回调回调
                else if ("UPDATE".equals(type)){
                    log.info("=========UPDATE=========");
                    JSONObject data = jsonObject.getJSONObject("data");
                    respones =  kuaidi100Handle.orderInfoModiHandle(data);
                }
                //快递单号打印回调
                else if ("FILLEXPNUM".equals(type) ){
                    log.info("=========FILLEXPNUM=========");
                    JSONArray data = jsonObject.getJSONArray("data");
                    respones =  kuaidi100Handle.kuaidiNum(data);
                }
                //快递物流更新
                else if ("EXPRESSINFO".equals(type)){
                    JSONObject data = jsonObject.getJSONObject("data");
                    kuaidi100Handle.expressInformation(data);
                } else {
                    //回调类型未知
                    log.info("=========default=========");
                    respones = kuaidi100Handle.kuaidiDefault(jsonObject);
                }
            }else {
                //签名不正确
                log.info("=========错误sign=======");
                respones = kuaidi100Handle.errorSign(jsonObject);
            }
            log.info("========快递回调响应==》{}《=====",respones);
            return respones;
        }
        return "{\"status\":200}";
    }

    public KuaidiProperties getKuaidiProperties() {
        return kuaidiProperties;
    }

    @Autowired
    public void setKuaidiProperties(KuaidiProperties kuaidiProperties) {
        this.kuaidiProperties = kuaidiProperties;
    }

    public Kuaidi100Handle getKuaidi100Handle() {
        return kuaidi100Handle;
    }
    @Autowired(required = false)
    public void setKuaidi100Handle( Kuaidi100Handle kuaidi100Handle) {
        this.kuaidi100Handle = kuaidi100Handle;
    }

    public boolean isErrorSign() {
        return errorSign;
    }

    public void setErrorSign(boolean errorSign) {
        this.errorSign = errorSign;
    }
}




















